package com.dl7.mvp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.RadioGroup;

import com.dl7.mvp.base.BaseActivity;
import com.dl7.mvp.mine.MineFragment;
import com.dl7.mvp.music.ui.MusicMainFragment;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.ActivityMainBinding;
import com.dl7.mvp.video.ui.VideoMainFragment;


public class MainActivity extends BaseActivity<ActivityMainBinding> implements RadioGroup.OnCheckedChangeListener {
    private Fragment[] mFragments;
    private String[] mTags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(R.string.app_name);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
        }
        initListener();
        initFragment();
    }

    private void initListener() {
        //对RadioGroup设置监听事件(监听点击选择)
        bindingView.radioGroup.setOnCheckedChangeListener(this);
    }

    private void initFragment() {
        //初始化要显示的Fragment数组
        mFragments = new Fragment[3];
        mTags = new String[3];

        mFragments[0] = new VideoMainFragment();
        mTags[0] = "video";
        mFragments[1] = new MusicMainFragment();
        mTags[1] = "music";
//        mFragments[2] = MineFragment.newInstance("three");
//        mTags[2] = "mine";

        //默认点击首页
        bindingView.radioGroup.check(R.id.radio_video);
        bindingView.radioGroup.setVisibility(View.GONE);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index = -1;
        switch (checkedId) {
            case R.id.radio_video:
                index = 0;
                break;
            case R.id.radio_music:
                index = 1;
                break;
            case R.id.radio_mine:
                index = 2;
                break;
        }
        if (index != -1) {
            hideAllFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = mFragments[index];
            if (!fragment.isAdded()) {
                transaction.add(R.id.fl_radio_show, fragment, mTags[index]);
            } else {
                transaction.show(fragment);
            }
            transaction.commit();
            getSupportFragmentManager().executePendingTransactions();
        }
    }

    //隐藏所有的fragment
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        VideoMainFragment videoFragment = (VideoMainFragment) getSupportFragmentManager().findFragmentByTag(mTags[0]);
        if (videoFragment != null) {
            transaction.hide(videoFragment);
        }
        MusicMainFragment musicFragment = (MusicMainFragment) getSupportFragmentManager().findFragmentByTag(mTags[1]);
        if (musicFragment != null) {
            transaction.hide(musicFragment);
        }
        MineFragment mineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag(mTags[2]);
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
        transaction.commit();
        getSupportFragmentManager().executePendingTransactions();
    }
}
